/**
  data-source.h - Created by Timothy Morey on 7/3/2012
*/

#ifndef __DATA_SOURCE_H
#define __DATA_SOURCE_H

#include <libxml/tree.h>

#include "data-schema.h"
#include "libnvn.h"

#define MAX_DATA_SOURCES 32

typedef int (*DataFrameCallback)(int dsid, const char* frame, int len);

typedef enum
{
  DataSourceTypeUnknown,    // Unknown data source.

  DataSourceTypeLocalFile,  // The data source reads from file on the local
                            // machine.  NOT IMPLEMENTED YET.

  DataSourceTypeOPeNDAP,    // The data source is an OPeNDAP server on a remote
                            // machine.  NOT IMPLEMENTED YET.

  DataSourceTypeNvnServer,  // The data source listens to a socket and accepts 
                            // frames from a remote instance of the nvn-server.

} DataSourceType;

typedef struct
{
  DataSourceType Type;

  char Dataset[256];    // For local files, this is just the file path.  For an
                        // OPeNDAP server, this is the data URL.  For an nvn-
                        // server, it has the form 
                        //   <servername>:<port>/<datasetname>.

  char Varname[256];

  int LocalPort;        // The nvn-server needs to deliver to an open port on
                        // this system.  If this is set to 0, then we will find
                        // an arbitrary open port.  Since that tactic can bee
                        // problematic with firewalls, we also let the port
                        // assignment come down from above.

} DataSourceDescriptor;

typedef struct
{
  int ID;
  DataSourceDescriptor Descriptor;
  DataSourceType Type;
  DataFrameCallback Callback;
} DataSource;

EXPORT int DataSourceGetDatasetSchema(DataSourceDescriptor desc,
                                      DataSetSchema* schema);

EXPORT int DataSourceGetGridSchema(DataSourceDescriptor desc,
                                   DataGridSchema* schema);

EXPORT int DataSourceStartStream(DataSourceDescriptor desc,
                                 DataFrameCallback callback,
                                 int* dsid);

EXPORT int DisconnectDataSource(int dsid);

EXPORT int GetRegisteredDataSource(int dsid, DataSource** datasource);

EXPORT int ParseDataSourceDescriptor(xmlNodePtr root, DataSourceDescriptor* desc);

EXPORT int RegisterDataSource(DataSource* datasource, int* dsid);

EXPORT int SaveDataSourceDescriptor(DataSourceDescriptor desc, xmlNodePtr* root);

EXPORT int UnregisterDataSource(int dsid);

#endif